const compare = (attr, order) => {
  return function (a, b) {
    let m = a[attr],
      n = b[attr];
    if (m < n) {
      return order * -1;
    }
    if (m > n) {
      return order * 1;
    }
    return 0;
  }
}

const convert = (list, attr, order) => {
  let folder = [], file = [], i = 0, len = list.length;
  for (; i < len; i++) {
    let type = list[i].type
    type == 'FILE' ? file.push(list[i]) : folder.push(list[i])
  }
  folder = folder.sort(compare(attr, order))
  file = file.sort(compare(attr, order))
  return [...folder, ...file]
}


// 格式化文件大小单位
const formatSize = (size, units) => {
  if (size == undefined) {
    return '-'
  } else {
    let unit;
    units = units || ['B', 'KB', 'M', 'G', 'TB'];
    while ((unit = units.shift()) && size > 1024) {
      size = size / 1024;
    }
    return (unit === 'B' ? size : Math.floor(size * 100) / 100) + unit;
  }
}

export { convert, formatSize }

